home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / xcmd / xrulesde.sit / Xrules™ Tutorial / card_10305.txt < prev    next >
Encoding:
Text File  |  1991-06-24  |  10.1 KB  |  175 lines

  1. -- card: 10305 from stack: in
  2. -- bmap block id: 10722
  3. -- flags: 4000
  4. -- background id: 7050
  5. -- name: 1-Backward Chaining
  6.  
  7.  
  8. -- part 1 (field)
  9. -- low flags: 01
  10. -- high flags: 0007
  11. -- rect: left=2 top=26 right=314 bottom=511
  12. -- title width / last selected line: 0
  13. -- icon id / first selected line: 0 / 0
  14. -- text alignment: 0
  15. -- font id: 22
  16. -- text size: 10
  17. -- style flags: 0
  18. -- line height: 13
  19. -- part name: doc
  20.  
  21.  
  22. -- part 2 (button)
  23. -- low flags: 00
  24. -- high flags: 8003
  25. -- rect: left=23 top=319 right=336 bottom=96
  26. -- title width / last selected line: 0
  27. -- icon id / first selected line: 0 / 0
  28. -- text alignment: 1
  29. -- font id: 0
  30. -- text size: 12
  31. -- style flags: 0
  32. -- line height: 16
  33. -- part name: sample rb
  34. ----- HyperTalk script -----
  35. on mouseUp
  36.   push this card
  37.   go to card "sample rb"
  38. end mouseUp
  39.  
  40.  
  41.  
  42. -- part contents for card part 1
  43. ----- text -----
  44. A backward chaining system begins with a goal and tries to prove that goal by ascertaining facts that can establish it.  In trying to determine the goal, it examines all queries and rules that could assert the goal in question until it either reaches a conclusion or exhausts the rules.  The order of the rules in the rule base and, within the rules, the order of the facts determines the search order. 
  45.  
  46. The logic for backward chaining can be expressed as follows:
  47.  
  48. main:
  49.   get goal (fact to be determined) from user
  50.   process fact(goal)
  51. end main
  52.  
  53. process fact(fact)
  54.   if fact has a query then
  55.     prompt user and get answer
  56.     assert fact
  57.   else
  58.     set pointer to top of rule base
  59.     while rules remain and fact not asserted
  60.       get next rule
  61.       if rule can assert fact then
  62.         while clauses remain
  63.           if fact in clause asserted then
  64.             evaluate clause
  65.           else
  66.             process fact(unknown fact)
  67.           end if
  68.         end while
  69.         if rule proved true then
  70.           assert fact
  71.           process daemons(fact)
  72.           if rule draws conclusion
  73.             post conclusion
  74.           end if
  75.         end if
  76.       end if
  77.     end while
  78.   end if
  79. end process fact
  80.  
  81. Note that the call to "process fact" within the routine "process fact" is a recursive call and that it passes in the unknown fact.  Thus, when a backward chaining system encounters an unknown fact while evaluating a rule, it will attempt to assert that fact.  Typically, the fact that is the goal of the system will not have a query associated with it but will have multiple rules capable of asserting the fact (goal).
  82.  
  83. We will now walk through a session with our sample rule base. In our sample, fact "model" is the goal.  The system will evaluate each of the rules capable setting fact "model" (our goal) until either one asserts fact "model" or it exhausts the possibilities. 
  84.  
  85. You may walk through this example using the shell.  Go to the "tutorial shell" card.  Click on the "load RB" button to make certain that the rule base is loaded in the "Rule Base" field.  Next if the "Forward Chain/Backward Chain" field does not show "Backward Chain", click on it to toggle it to "Backward Chain". 
  86.  
  87. If you would like to monitor the process, click on the "trace" check box to set trace on (an X will show in the box when it is on).  To step through the process, click on the "step" check box to set step on (an X will show in the box when it is on).
  88.  
  89. Next, click on the "(re)start" button to start the session and answer the questions or follow the directions as they appear in the "Query" field.  You should enter "model" in response to the "Enter goal..." query.  During the session you may toggle between this card and others in the stack.  The shell will retain its state when you toggle among cards; you do not have to start over each time that you return to the shell.
  90.  
  91. If you checked both trace and step, watch the "Trace" field to see the actions taken by the system.  The system will show the state of the stack in the "Stack" field and the facts and their values in the "Facts" field. 
  92.  
  93. NOTE:  The system handles daemons and traces actions with respect to them.  You will notice that whenever a fact is asserted, that fact is pushed on the stack while the system looks for daemons associated with it.  Therefore, the fact name will appear twice on the stack during that processing.  For this tutorial, ignore these actions taken by the system concerning daemons.
  94.  
  95. The system begins by pushing the goal "model" on the stack.  It then searches for queries or rules which can establish that goal, or fact.  It first finds rule "Corvette", pushes that rule on the stack, and attempts to evaluate it.  Since the first clause, "rule car", requires fact "car" which has not been asserted, the system pushes fact "car" on the stack.
  96.  
  97. The system then searches for a query or rule asserting fact "car".  It finds rule "car 1" and pushes it on the stack.  In evaluating rule "car 1" the system finds that fact "wheels" has not been asserted and pushes it on the stack. 
  98.  
  99. Fact wheels has a query associated with it, so the system asks "How many wheels does it have?". Answer "4".  The system asserts fact "wheels = 4", processes daemons, and pops fact "wheels" from the stack.
  100.  
  101. The system resumes evaluation of rule "car 1" and is now able to evaluate the clause concerning "wheels".  However, it finds that fact "passengers" has not been asserted, so it pushes fact "passengers" on the stack. 
  102.  
  103. Fact "passengers" has a query associated with it, so the system asks "How many passengers can it carry?". Answer "4".  The system asserts fact "passengers = 4", processes daemons, and pops fact "passengers" from the stack.
  104.  
  105. The system then continues evaluating rule "car 1" and completes the clause concerning "passengers".  However, it finds that fact "used for cargo" has not been asserted, so it pushes fact "used for cargo" on the stack. 
  106.  
  107. Fact "used for cargo" has a query associated with it, so the system asks "Is it used to carry cargo?".  Answer "no".  The system asserts fact "used for cargo = no", processes daemons, and pops fact "used for cargo" from the stack.
  108.  
  109. Now the system is able to complete evaluation of rule "car 1".  It finds the rule true and executes the "then" part of the rule, asserting fact "car = true", processing daemons, and popping rule "car 1" from the stack.  Since fact "car" has been asserted, it is also popped from the stack.
  110.  
  111. The system resumes evaluation of rule "Corvette" and finds that fact "make" has not been asserted, so it pushes fact "make" on the stack.
  112.  
  113. Fact "make" has a query associated with it, so the system asks "What is the make of the vehicle?".  Answer "Chevy".  The system asserts fact "make = Chevy", processes daemons, and pops fact "make" from the stack. 
  114.  
  115. The system continues evaluating rule "Corvette" finding the clause concerning 
  116. "make" true.  However, it finds that fact "passengers" does not match the clause.  Therefore, the rule "Corvette" proves false and, since there is no ELSE clause, it is popped from the stack.
  117.  
  118. The system now searches for the next rule capable of asserting "model", finds rule "Camaro" and pushes it on the stack.  It evaluates rule "Camaro", finds that it proves true, and executes the "then" part of the rule.  From the "then" part of the rule, the system asserts the fact "model = Camaro", processes daemons, and displays the conclusion "The car is a Camaro".  Since rule "Camaro" is finished, the system pops it from the stack. Since fact "model" has been resolved, the system pops it from the stack.
  119.  
  120. If the user chooses to continue, the system will not continue the search because the stack is empty.
  121.  
  122. After walking through the tutorial for this case, add a query for the fact "car" and watch the response.  Add the query to the fact declaration section as shown.
  123.  
  124.   fact
  125.     car
  126.       "the vehicle is a car, true or false?",
  127.     passengers
  128.       "How many passengers can it carry?",
  129.     wheels
  130.       "How many wheels does it have",
  131.     "used for cargo"
  132.       "Is it used to carry cargo?",
  133.     make
  134.       "What is the make of the vehicle?"
  135.  
  136. The query for fact "car" will wrap in the "Rule Base" field on the "tutorial shell" card.  You should let it wrap.  DO NOT place a carriage return inside of the quotes.
  137.  
  138. Now click on "(re)start" and begin the session (do not click on "load RB" or it will restore the rule base to its original condition).  During the consultation, you will see that the system will ask "the vehicle is a car, true or false?" when trying to determine the fact "car".  If you answer the question (answer "true" or "false"), it will not ask about facts "wheels" and "used for cargo". It will, however, ask about "passengers" when that fact is needed for other rules.  If you enter only a carriage return (indicating that you do not know the answer), it will ask about facts "wheels" and "used for cargo" in order to determine fact "car".
  139.  
  140. Now let's force the system to examine all rules when attempting to find "model".  To do that we must change the "put-into" statement for the rules "Corvette",
  141. "Camaro", and "Mustang" to set fact "model" to the null string as shown (click on "load RB" first to restore the original rule base).
  142.  
  143.   rule Corvette
  144.   if 
  145.     fact car and
  146.     fact make = Chevy and
  147.     fact passengers = 2
  148.   then 
  149.     put "" into fact model
  150.     conclude "The car is a Corvette."
  151.  
  152.   rule Camaro
  153.   if 
  154.     fact car and
  155.     fact make = Chevy and
  156.     fact passengers = 4
  157.   then 
  158.     put "" into fact model
  159.     conclude "The car is a Camaro."
  160.  
  161.   rule Mustang
  162.   if 
  163.     fact car and
  164.     fact make = Ford
  165.   then 
  166.     put "" into fact model
  167.     conclude "The car is a Mustang."
  168.  
  169. Now, run the system (don't forget to "(re)start" and answer as indicated in the detailed example above.  You will notice that if you continue after it has found the model to be a Camaro, that it will examine the rule "Mustang".  This is because the system considers a fact containing null ("") not asserted, so it will continue trying to assert that fact. 
  170.  
  171. Try it again, only answer "*" when asked about the number of passengers.  This will cause fact "passengers" to match anything and you will find that it will conclude that the car could be a Corvette or a Camaro.
  172.  
  173. Feel free to modify the rule base on the "tutorial shell" card and experiment with backward chaining.  If you wish to restore the original sample rule base, simply click on the "load RB" button. 
  174.  
  175.